#import "CQConnectionTableCell.h"

@implementation CQConnectionTableCell
- (id) init {
	self = [super init];
	if( ! self ) return nil;

	[self setImage:[UIImage applicationImageNamed:@"server.png"]];
	[self setShowDisclosure:YES];
	[self setDisclosureClickable:YES];
	[self setDisclosureStyle:1];

	_nicknameLabel = [[UILabel alloc] init];
	_timeLabel = [[UILabel alloc] init];
	_badgeImageView = [[UIImageView alloc] init];

	[self addSubview:_nicknameLabel];
	[self addSubview:_timeLabel];
	[self addSubview:_badgeImageView];

	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	float grayComponents[4] = { 0.6, 0.6, 0.6, 1. };
	float whiteComponents[4] = { 1., 1., 1., 1. };
	float transparentWhiteComponents[4] = { 1., 1., 1., 0. };

/*	[_nicknameLabel setFont:[WebFontCache createFontWithFamily:@"Helvetica" traits:0 size:14.]];
	[_nicknameLabel setColor:CGColorCreate(colorSpace, grayComponents)];
	[_nicknameLabel setHighlightedColor:CGColorCreate(colorSpace, whiteComponents)];
	[_nicknameLabel setBackgroundColor:CGColorCreate(colorSpace, transparentWhiteComponents)];*/

	float blueComponents[4] = { 0.19607843, 0.29803922, 0.84313725, 1. };

	[_timeLabel setFont:[WebFontCache createFontWithFamily:@"Helvetica" traits:2 size:14.]];
	[_timeLabel setColor:CGColorCreate(colorSpace, blueComponents)];
	[_timeLabel setHighlightedColor:CGColorCreate(colorSpace, whiteComponents)];
	[_timeLabel setBackgroundColor:CGColorCreate(colorSpace, transparentWhiteComponents)];

	return self;
}

- (struct CGPoint) iconInset {
	CGPoint inset = [super iconInset];
	inset.y -= 2.;
	inset.x += 7;
	return inset;
}

- (void) layoutSubviews {
	[super layoutSubviews];

	UIImageView *imageView = [self iconImageView];
	UILabel *titleLabel = [self titleTextLabel];

	CGRect frame = [imageView frame];
	frame.origin.y += 3;
	frame.origin.x -= 18;
	frame.size.width = 41.;
	frame.size.height = 41.;
	[_badgeImageView setFrame:frame];

	frame = [titleLabel frame];
	frame.origin.y -= 8.;
	frame.size.width = [self frame].size.width - frame.origin.x - 40.;

	if( [self isRemoveControlVisible] )
		frame.size.width -= [[self removeControl] frame].size.width - 10.;

	[titleLabel setFrame:frame];

	frame.origin.y += 16.;
	[_nicknameLabel setFrame:frame];

	[_timeLabel sizeToFit];

	frame = [titleLabel frame];
	frame.origin.y += 8.;
	frame.origin.x = [self frame].size.width - [_timeLabel frame].size.width - 45.;
	[_timeLabel setFrame:frame];
}

- (void) updateHighlightColors {
	[super updateHighlightColors];

	UILabel *titleLabel = [self titleTextLabel];
	[_nicknameLabel setHighlighted:[titleLabel isHighlighted]];
	[_timeLabel setHighlighted:[titleLabel isHighlighted]];
}

- (void) removeControlWillHideRemoveConfirmation:(UITableViewCellRemoveControl *) control {
	[super removeControlWillHideRemoveConfirmation:control];

	UIAlphaAnimation *animation = [[UIAlphaAnimation alloc] initWithTarget:_timeLabel];
	[animation setStartAlpha:0.];
	[animation setEndAlpha:1.];

	[[UIAnimator sharedAnimator] addAnimation:animation withDuration:0.2 start:YES];

	[animation release];
}

- (void) removeControlWillShowRemoveConfirmation:(UITableViewCellRemoveControl *) control {
	[super removeControlWillShowRemoveConfirmation:control];

	UIAlphaAnimation *animation = [[UIAlphaAnimation alloc] initWithTarget:_timeLabel];
	[animation setStartAlpha:1.];
	[animation setEndAlpha:0.];

	[[UIAnimator sharedAnimator] addAnimation:animation withDuration:0.2 start:YES];

	[animation release];
}

- (UILabel *) nicknameTextLabel {
	return _nicknameLabel;
}

- (void) setNickname:(NSString *) text {
	[_nicknameLabel setText:text];
}

- (NSString *) nickname {
	return [_nicknameLabel text];
}

- (void) setTime:(NSString *) text {
	[_timeLabel setText:text];
	[self layoutSubviews];
}

- (NSString *) time {
	return [_timeLabel text];
}

- (void) setStatus:(CQConnectionTableCellStatus) status {
	_status = status;

	if (_status == CQConnectionTableCellNotConnectedStatus)
		[_badgeImageView setImage:nil];
	else if (_status == CQConnectionTableCellServerDisconnectedStatus)
		[_badgeImageView setImage:[UIImage applicationImageNamed:@"errorBadgeDim.png"]];
	else if (_status == CQConnectionTableCellConnectingStatus)
		[_badgeImageView setImage:[UIImage applicationImageNamed:@"connectingBadgeDim.png"]];
	else if (_status == CQConnectionTableCellConnectedStatus)
		[_badgeImageView setImage:[UIImage applicationImageNamed:@"connectedBadgeDim.png"]];
}

- (CQConnectionTableCellStatus) status {
	return _status;
}
@end
